package com.mp.core.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author sumory.wu
 * @date 2011-9-17 上午12:23:15
 * @version 2.0
 */
public class DateUtils {

    public final static String formatStr_yyyyMMddHHmmss = "yyyy-MM-dd HH:mm:ss";
    public final static String formatStr_yyyyMMddHHmm = "yyyy-MM-dd HH:mm";
    public final static String formatStr_yyyyMMddHH = "yyyy-MM-dd HH";
    public final static String formatStr_yyyyMMdd = "yyyy-MM-dd";
//    public final static String formatStr_yyyy年MM月dd日HHmm = "yyyy年MM月dd日 HH:mm";
    public final static String formatStr_MMdd_EE_HHmm = "MM/dd EEEE HH:mm";
    public final static String formatStr_HHmm = "HH:mm";
    public final static String formatStr_MMdd = "MM/dd";
    public final static String formatStr_MMdd2 = "MM月dd日";
    public final static String formatStr_MMddHHmm = "MM-dd HH:mm";
    public final static String formatStr_yyyyMMddHHmmss1 = "yyyy/MM/dd HH:mm:ss";
    public final static String formatStr_yyyyMMddHHmmss2 = "yyyyMMddHHmmss";
    public final static String formatStr_HHMMSS = "HH:mm:ss";
    public final static String formatStr_MM = "MM";
    public static String DateToString(long timestamp, String toFormatStr) {
        try {
            return new SimpleDateFormat(toFormatStr).format(timestamp);
        }
        catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    public static String DateToString(Date date, String toFormatStr) {
        try {
            return new SimpleDateFormat(toFormatStr).format(date);
        }
        catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    public static String DateToString(java.sql.Date date, String toFormatStr) {
        try {
            return new SimpleDateFormat(toFormatStr).format(date);
        }
        catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    public static Date StringToDate(String dateString, String strFormat) {
        try {
            Date date = new SimpleDateFormat(strFormat).parse(dateString);
            return date;
        }
        catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }



    public static java.sql.Timestamp stringToTimestamp(String dateString, String strFormat) {
        return new java.sql.Timestamp(StringToDate(dateString,strFormat).getTime());
    }

    public static java.sql.Timestamp stringToTimestampAddDays(String dateString, String strFormat,int days) {
        return new java.sql.Timestamp(StringToDate(dateString,strFormat).getTime() + days*1000*60*60*24);
    }


    public static java.sql.Date toSQLDate(Date date) {
        return new java.sql.Date(date.getTime());
    }

    public static java.sql.Timestamp toTimestamp(Date date) {
        return new java.sql.Timestamp(date.getTime());
    }

    public static java.sql.Timestamp toTimestamp(java.sql.Date date) {
        return new java.sql.Timestamp(date.getTime());
    }

    public static Date toUtilDate(java.sql.Date date) {
        return new Date(date.getTime());
    }


    /**
     * 得到 前/后 几天的时间
     * @author zhangjunhao 2015-1-30 上午11:10:37 
     *
     * @param day
     * @param now
     * @param format
     * @return
     */
    public static String getSomeDate(int day, Date now, String format) {
        try {
            Calendar date = Calendar.getInstance();
            date.setTime(now);
            date.add(Calendar.DATE, day);
            return DateToString(date.getTime(), format);
        }
        catch (Exception e) {
            e.printStackTrace();
            return "";
        }

    }

    /**
     * 得到几天前的时间
     *
     * @param day
     * @return
     */
    public static String getSomeDate(int day) {
        try {
            Calendar date = Calendar.getInstance();
            date.add(Calendar.DATE, -day);// 向前推day
            return DateToString(date.getTime(), "yyyy-MM-dd");
        }
        catch (Exception e) {
            e.printStackTrace();
            return "";
        }

    }

    /**
     * 比较时间是否是同一天
     * @author zhangjunhao 2015-3-14 下午2:21:16
     *
     * @param date1
     * @param date2
     * @return [1, 2015, 2, 11, 2, 14, 73, 7, 2, 0, 0, 0, 0, 0, 0, 28800000, 0]
     */
    public static boolean compareEqualDate(Date date1, Date date2) {
        try {
			/*Calendar c1 = Calendar.getInstance();
			Calendar c2 = Calendar.getInstance();
			c1.setTime(date1);
			c2.setTime(date2);
			c1.set(c1.get(Calendar.YEAR), c1.get(Calendar.MONTH), c1.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
			c2.set(c2.get(Calendar.YEAR), c2.get(Calendar.MONTH), c2.get(Calendar.DAY_OF_MONTH), 0, 0, 0);*/
            // 如果比赛时间在他前两个小时前面
            //c2.add(Calendar.HOUR, -2);
            String c1 = DateToString(date1, formatStr_yyyyMMdd);
            String c2 = DateToString(date2, formatStr_yyyyMMdd);
            return c1.equals(c2);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 比较时间
     * @author zhangjunhao 2015-3-4 下午4:38:01
     *
     * @param date1
     * @param date2
     * @return false: { date1 < date2 }, true: { date1 > date2 }
     */
    public static boolean compareDate(Date date1, Date date2) {
        try {
            Calendar c1 = Calendar.getInstance();
            Calendar c2 = Calendar.getInstance();
            c1.setTime(date1);
            c2.setTime(date2);
            // 如果比赛时间在他前两个小时前面
            //c2.add(Calendar.HOUR, -2);
            if (c1.before(c2)) {
                return false;
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return true;
        }
    }

    /**
     * 选择两个时间中较大的一个
     * @param date1
     * @param date2
     * @return
     */
    public static String getMaxDate(String date1, String date2){
        Date comDate1 = StringToDate(date1, formatStr_yyyyMMdd);
        Date comDate2 = StringToDate(date2, formatStr_yyyyMMdd);
        if(comDate1.after(comDate2)){
            return date1;
        }else{
            return date2;}
    }

    /**
     * 年份计算
     * @param date
     * @param year
     * @return
     */
    public static String getDateByYear(Date date, int year){
        try {
            Calendar cal=Calendar.getInstance();
            cal.setTime(date);
            cal.add(Calendar.YEAR, year);//
            return DateToString(cal.getTime(), "yyyy-MM-dd");
        }
        catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    /**
     * 得到两个时间的月份差
     *@author lanyuping
     * @param date1
     * @param date2
     * @return
     * @throws ParseException
     *@data 2016年9月7日 下午3:11:52
     */
    public static Integer getMonthSpace(String date1, String date2)throws ParseException {
        Integer result = 0;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c1 = Calendar.getInstance();
        Calendar c2 = Calendar.getInstance();
        c1.setTime(sdf.parse(date1));
        c2.setTime(sdf.parse(date2));
        result = c2.get(Calendar.MONTH) - c1.get(Calendar.MONTH);
        return result == 0 ? 1 : Math.abs(result);

    }

    /**
     * 获取系统当前时间
     * @return
     */
    public static String getNowTime(String type) {
        SimpleDateFormat df = new SimpleDateFormat(type);
        return df.format(new Date());
    }

    public static void main(String[] args) {
        System.out.println(DateUtils.DateToString(new Date(), "YYYY-MM-DD"));
        System.out.println(DateUtils.DateToString(new Date(), "yyyy-MM-dd"));
    }
}
